<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="" prop="doctorName">
        <el-input v-model="queryParams.doctorName" placeholder="请输入医生姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="" prop="hospId">
        <el-input v-model="queryParams.hospId" placeholder="请输入医院名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item label="" prop="hospId">
        <el-select v-model="queryParams.hospId" filterable placeholder="请选择医院">
          <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="" prop="operateDeptId" v-if="queryParams.hospId">
          <el-select v-model="queryParams.operateDeptId" filterable placeholder="请选择所属科室" class="wp100">
            <el-option v-for="item in deptAll" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>


      <el-form-item label="" prop="doctorTitle">
        <el-select v-model="queryParams.doctorTitle" filterable placeholder="请输入专业职称" class="wp100">
            <el-option v-for="dict in dict.type.doctor_title" :key="dict.value" :label="dict.label" :value="dict.value">
            </el-option>
          </el-select>

        <!-- <el-input v-model="queryParams.doctorTitle" placeholder="请输入专业职称" clearable @keyup.enter.native="handleQuery" /> -->
      </el-form-item>
      <!-- <el-form-item label="教学职称" prop="teachingTitle">
        <el-input v-model="queryParams.teachingTitle" placeholder="请输入教学职称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="荣誉称号" prop="honoraryTitle">
        <el-input v-model="queryParams.honoraryTitle" placeholder="请输入荣誉称号" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="医生码" prop="doctorCode">
        <el-input v-model="queryParams.doctorCode" placeholder="请输入医生码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['hcem:doctor:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['hcem:doctor:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['hcem:doctor:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['hcem:doctor:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="doctorList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="医生统一码" align="center" prop="doctorCode" /> -->
      <el-table-column label="医院" align="center" prop="hospitalName" />
      <el-table-column label="科室" align="center" prop="operateDeptId">
        <template slot-scope="scope">
            {{ findMyDeptlabel(scope.row.operateDeptId) }}
        </template>
      </el-table-column>

      <el-table-column label="医生姓名" align="center" prop="doctorName" />
      <!-- 没有科室 -->
      <el-table-column label="专业职称" align="center" prop="doctorTitleLabel" />
      <!-- <el-table-column label="专业职称" align="center" prop="doctorTitle" /> -->
      <el-table-column label="性别" align="center" prop="sex">
        <template slot-scope="scope">
          {{ findSex(scope.row.sex)}}
        </template>
      </el-table-column>
      <el-table-column label="擅长" align="center" prop="goodAt" :show-overflow-tooltip="true"/>
      <!-- <el-table-column label="排序" align="center" prop="sort" /> -->
      <!-- <el-table-column label="简介" align="center" prop="detail" :show-overflow-tooltip='true' /> -->
      <!-- <el-table-column label="教学职称" align="center" prop="teachingTitle" />
      <el-table-column label="荣誉称号" align="center" prop="honoraryTitle" />
      <el-table-column label="运营科室id" align="center" prop="operateDeptId" />
      <el-table-column label="社会职务" align="center" prop="socialPosition" />
      <el-table-column label="学历" align="center" prop="xueli" />
      <el-table-column label="专业方向" align="center" prop="majorDirection" />
      <el-table-column label="疾病标签" align="center" prop="illnessLabel" />
      <el-table-column label="其他标签" align="center" prop="elseLabel" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['hcem:doctor:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['hcem:doctor:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改运营医生信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="75%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <!-- <section class="section">医生所属科室</section> -->
        <el-form-item label="所属医院" prop="hospId">
          <el-select v-model="form.hospId" filterable @change="myHosp" placeholder="请选择医院" class="wp100">
            <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属科室" prop="operateDeptId" v-if="form.hospId">
          <el-select v-model="form.operateDeptId" filterable placeholder="请选择所属科室" class="wp100">
            <el-option v-for="item in deptAll" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="医生码" prop="doctorCode">
          <el-input v-model="form.doctorCode" placeholder="请输入医生码" />
        </el-form-item> -->
        <el-form-item label="医生姓名" prop="doctorName">
          <el-input v-model="form.doctorName" placeholder="请输入医生姓名" />
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select v-model="form.sex" filterable  placeholder="请选择性别" class="wp100">
            <el-option v-for="item in sexOptions" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="专业职称" prop="doctorTitle">
          <el-select v-model="form.doctorTitle" filterable placeholder="请输入专业职称" class="wp100">
            <!-- v-for="dict in dict.type.doctor_title" :key="dict.value" :label="parseInt(dict.value) -->
            <el-option v-for="dict in dict.type.doctor_title" :key="dict.value" :label="dict.label" :value="Number(dict.value)">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="医生工号" prop="no">
          <el-input v-model="form.no" placeholder="请输入医生工号" />
        </el-form-item>
        <!-- <el-form-item label="医院名称" prop="hospId">
          <el-input v-model="form.hospId" placeholder="请输入医院名称" />
        </el-form-item> -->
        <!-- <el-form-item label="医生头像" prop="avatar">
          <el-input v-model="form.avatar" type="textarea" placeholder="请输入内容" />
        </el-form-item> -->

      

        <el-form-item label="擅长" prop="goodAt">
          <el-input v-model="form.goodAt" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="简介" prop="detail">
          <el-input v-model="form.detail" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      
        <!-- <el-form-item label="运营科室id" prop="operateDeptId">
          <el-input v-model="form.operateDeptId" placeholder="请输入运营科室id" />
        </el-form-item> -->
        <!-- <el-form-item label="排序" prop="sort">
          <el-input v-model="form.sort" placeholder="请输入排序" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDoctor, getDoctor, delDoctor, addDoctor, updateDoctor, getDeptList } from "@/api/hcem/doctor";
import { hosList } from '@/api/hcem/title'
export default {
  name: "Doctor",
  dicts: ['rz_info', 'doctor_title'],
  data() {
    return {
      options: [],//医院列表选项
      deptAll: [],   //医院所属科室列表
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      sexOptions: [
        { value: 9, label: "未知" },
        { value: 1, label: "女" },
        { value: 2, label: "男" },
      ],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 运营医生信息表格数据
      doctorList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        doctorCode: null,
        no: null,
        hospId: null,
        avatar: null,
        doctorName: null,
        sex: null,
        doctorTitle: null,
        goodAt: null,
        detail: null,
        teachingTitle: null,
        honoraryTitle: null,
        awards: null,
        cardNo: null,
        birthDate: null,
        phone: null,
        email: null,
        operateDeptId: null,
        sort: null,
        workStartDate: null,
        rzInfo: null,
        socialPosition: null,
        xueli: null,
        majorDirection: null,
        illnessLabel: null,
        elseLabel: null,
        hospitalCode: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        hospitalCode: [
          { required: false, message: "所属医院名称称不能为空", trigger: "blur" }
        ],
        doctorCode: [
          { required: false, message: "医生码不能为空", trigger: "blur" }
        ],
        no: [
          { required: false, message: "医生工号不能为空", trigger: "blur" }
        ],
        hospId: [
          { required: true, message: "医院名称不能为空", trigger: "blur" }
        ],
        avatar: [
          { required: false, message: "医生头像不能为空", trigger: "blur" }
        ],
        doctorName: [
          { required: true, message: "医生姓名不能为空", trigger: "blur" }
        ],
        sex: [
          { required: true, message: "性别不能为空", trigger: "change" }
        ],
        doctorTitle: [
          { required: true, message: "专业职称不能为空", trigger: "blur" }
        ],
        goodAt: [
          { required: false, message: "擅长不能为空", trigger: "blur" }
        ],
        detail: [
          { required: false, message: "简介不能为空", trigger: "blur" }
        ],
        teachingTitle: [
          { required: false, message: "教学职称不能为空", trigger: "blur" }
        ],
        honoraryTitle: [
          { required: false, message: "荣誉称号不能为空", trigger: "blur" }
        ],
        awards: [
          { required: false, message: "专业获奖情况不能为空", trigger: "blur" }
        ],
        cardNo: [
          { required: false, message: "身份证号不能为空", trigger: "blur" }
        ],
        birthDate: [
          { required: false, message: "出生日期不能为空", trigger: "blur" }
        ],
        phone: [
        {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur",
            required: true,
          }
          // { required: true, message: "手机号不能为空", trigger: "blur" }
        ],
        email: [
          { required: false, message: "邮箱不能为空", trigger: "blur" }
        ],
        operateDeptId: [
          { required: true, message: "运营科室id不能为空", trigger: "blur" }
        ],
        sort: [
          { required: true, message: "排序不能为空", trigger: "blur" }
        ],
        workStartDate: [
          { required: false, message: "开始工作时间不能为空", trigger: "blur" }
        ],
        rzInfo: [
          { required: false, message: "认证信息不能为空", trigger: "blur" }
        ],
        socialPosition: [
          { required: false, message: "社会职务不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: false, message: "创建时间不能为空", trigger: "blur" }
        ],
        updateTime: [
          { required: false, message: "更新时间不能为空", trigger: "blur" }
        ],
        xueli: [
          { required: false, message: "学历不能为空", trigger: "blur" }
        ],
        majorDirection: [
          { required: false, message: "专业方向不能为空", trigger: "blur" }
        ],
        illnessLabel: [
          { required: false, message: "疾病标签不能为空", trigger: "blur" }
        ],
        elseLabel: [
          { required: false, message: "其他标签不能为空", trigger: "blur" }
        ]
      }
    };
  },
  async created() {
    await this.getHosList()
    await this.getList();
  },
  computed: {
    findMyDeptlabel() {
      return (depValue) => {
        if (depValue) {
          let findDept = this.deptAll.find(val => val.value == depValue)
          if(findDept !== undefined){
            return findDept.label
          }else{
            return '科室ID错误'
          }
        } else {
          return '暂无'
        }
      }
    },
    findSex(){
      return (sexValue)=>{
        if(sexValue == 2) return '男'
        if(sexValue == 1) return '女'
        if(sexValue == 0) return '未知'
      }
    }
  },
  methods: {
    /** 查询运营医生信息列表 */
    getList() {
      this.loading = true;
      listDoctor(this.queryParams).then(response => {
        this.doctorList = response.rows;
        if(this.doctorList !== undefined && this.doctorList.length > 0){
          this.myHosp(this.doctorList[0].hospId)
        }
        this.total = response.total;
        this.loading = false;
      });
    },
    myHosp(val) {
      let parms = { hospitalCode: val }
      if(val) this.queryParams.hospId = val
      console.log(val,111);
      
      this.getMyDeptList(parms)
      // console.log(val,111);
    },
    async getMyDeptList(parms) {
      let res = await getDeptList(parms)
      if (res.code == 200) {
        this.deptAll = res.data.map(v => {
          return {
            value: v.id,
            label: v.operateDeptName
          }
        })
        //console.log(this.deptAll);
      }
    },
    async getHosList() {  // 获取医院列表
      let res = await hosList()
      let data = res.data
      data.forEach(v => {
        this.options.push({
          value: v.hospital_code,
          label: v.hospital_name
        })
      })
      // console.log(res);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        hospitalCode: null,
        id: null,
        doctorCode: null,
        no: null,
        hospId: null,
        avatar: null,
        doctorName: null,
        sex: null,
        doctorTitle: null,
        goodAt: null,
        detail: null,
        teachingTitle: null,
        honoraryTitle: null,
        awards: null,
        cardNo: null,
        birthDate: null,
        phone: null,
        email: null,
        operateDeptId: null,
        sort: null,
        workStartDate: null,
        rzInfo: [],
        socialPosition: null,
        createTime: null,
        updateTime: null,
        xueli: null,
        majorDirection: null,
        illnessLabel: null,
        elseLabel: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加医生信息";
    },
    /** 修改按钮操作 */
    async handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids

      getDoctor(id).then(response => {
        this.form = response.data;
        this.form.rzInfo = this.form.rzInfo.split(",");
        this.open = true;
        this.title = "修改医生信息";
        let parms = { hospitalCode: this.form.hospitalCode }
        this.getMyDeptList(parms)
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.rzInfo = this.form.rzInfo.join(",");
          if (this.form.id != null) {
            updateDoctor(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDoctor(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除运营医生信息编号为"' + ids + '"的数据项？').then(function () {
        return delDoctor(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('hcem/doctor/export', {
        ...this.queryParams
      }, `doctor_${new Date().getTime()}.xlsx`)
    }
  }
};

</script>
<style scoped lang="scss">
.section {
  font-size: 1.3em;
  font-weight: 600;
  padding-left: 10px;
  padding-bottom: 25px;
  position: relative;

  &:before {
    content: "";
    width: 6px;
    background-color: #409EFF;
    height: 20px;
    position: absolute;
    left: 0;
    top: 3px;
  }
}

.dialog-footer {
  text-align: center;
  padding-bottom: 40px;
}
</style>
